<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closeRoleDialog" @open="openRole">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="confirm">确定</el-button>
      <el-button size="small" @click="closeRoleDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employee'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      checkList: [],
      roleList: []
    }
  },
  methods: {
    async hGetRoleList() {
      const res = await getRoleList({ page: 1, pageSize: 100 })
      this.roleList = res.data.rows
    },
    closeRoleDialog() {
      this.$emit('close-roleDialog')
    },
    openRole() {
      this.hGetRoleList()
      this.hGetDetailg()
    },
    async confirm() {
      await assignRoles({ id: this.curId, roleIds: this.checkList })
      this.$message({
        type: 'success',
        message: '分配角色成功'
      })
      this.closeRoleDialog()
    },
    // 获取详情
    async hGetDetailg() {
      const res = await getUserDetailById(this.curId)
      this.checkList = res.data.roleIds
    }
  }
}
</script>
